home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / info-service / prospero / PRM / src / testprog / sortaux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-02  |  1.9 KB  |  122 lines

  1. /*
  2.  * Copyright (c) 1992, 1993 by the University of Southern California
  3.  *
  4.  * For copying and distribution information, please see the files
  5.  * <prm-copyr.h>.
  6.  * 
  7.  * Written by srao 9/92
  8.  */
  9.  
  10. #include <prm-copyr.h>
  11.  
  12.  
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15.  
  16. extern u_long my_tid;
  17. extern char *p_err_string;
  18.  
  19. qsort(a, m, n)
  20. float a[];
  21. int m, n;
  22. {
  23.   int i, j;
  24.   float pivot, tmp;
  25.  
  26.   if (m >= n) return;
  27.   i = m;
  28.   j = n+1;
  29.   pivot = a[m];
  30.       
  31.   for(;;) {
  32.     do 
  33.       i++;
  34.     while(a[i] < pivot);
  35.          
  36.     do 
  37.       j--;
  38.      while(a[j] > pivot);
  39.  
  40.     if (i < j) {
  41.       tmp = a[j];
  42.       a[j] = a[i];
  43.       a[i] = tmp;
  44.     }
  45.     else
  46.       break;
  47.   }
  48.   tmp = a[j];
  49.   a[j] = a[m];
  50.   a[m] = tmp;
  51.  
  52.   qsort(a, m, j-1);
  53.   qsort(a, j+1, n);
  54.        
  55. }
  56.  
  57.  
  58. merge(a, start, middle,  len)
  59. float a[];
  60. int start, middle, len;
  61. {
  62.   int i, j, m, n, ind;
  63.   float *b;
  64.  
  65.   i=start;
  66.   j=middle;
  67.   m= middle-1;
  68.   n = start+len-1;
  69.   b = (float *)calloc(len, sizeof(float));
  70.  
  71.   ind =0;
  72.   while( (i<=m) && (j<=n) ) {
  73.  
  74.     if (a[i] <= a[j]) 
  75.       *(b+ind) = a[i++];
  76.     else 
  77.       *(b+ind) = a[j++];
  78.  
  79.     ++ind;
  80.   }
  81.   if (i>m) {
  82.     bcopy(b, &a[start], (j-start)*sizeof(float));
  83.   }
  84.   else {
  85.     bcopy(&a[i], (b+ind), (m+1-i)*sizeof(float) );
  86.     bcopy(b, &a[start], len*sizeof(float));
  87.   }    
  88. }
  89.  
  90.  
  91. char *
  92. write_output_file(dfname, a, first, length, iter_num)
  93.  
  94. char dfname[];
  95. float a[];
  96. int first, length, iter_num;
  97. {
  98.  
  99.   char *outflname, extn[16], *dot;
  100.   int ofd, nbytes;
  101.  
  102.   outflname = (char *)malloc(80);
  103.   strcpy(outflname, dfname);
  104.   sprintf(extn,"inr%d", my_tid);
  105.   dot = (char *)index(outflname,'.');
  106.   if (dot) 
  107.     *(dot+1) = '\0';
  108.   else
  109.     strcat(outflname, ".");
  110.   strcat(outflname, extn);
  111.   
  112.   ofd = io_open(outflname, O_WRONLY|O_CREAT, 0x1B4);
  113.   nbytes = length*sizeof(float);
  114.   if (io_write(ofd, a + first, nbytes, 0) != nbytes) {
  115.     io_printf("write failed! iter=%d: %s", my_tid, iter_num, &p_err_string, 
  116.           (char *)0);
  117.     exit(1);
  118.   }
  119.   io_close(ofd);
  120.   return outflname;
  121. }
  122.